package leetcode;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.*;

public class LeetCodeMain108 {
    public int add(int a, int b) {
        while(b != 0) {
            int c = (a & b) << 1;
            a ^= b;
            b = c;
        }
        return a;
    }
    List<Integer> list=new ArrayList<>();
    public int kthLargest(TreeNode root, int k) {
        preOrder(root);
        return list.get(list.size()-k+1);
    }
    public void preOrder(TreeNode root){
        if (root==null){
            return;
        }
        preOrder(root.left);
        list.add(root.val);
        preOrder(root.right);
    }
    public String reverseWords(String s) {
        s = s.trim();
        List<String> wordList = Arrays.asList(s.split("\\s+"));
        Collections.reverse(wordList);
        return String.join(" ", wordList);
    }
    public int missingNumber1(int[] nums) {
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            if (i!=nums[i]){
                return i;
            }
        }
        return nums.length;
    }
    public ListNode reverseKGroup(ListNode head, int k) {
        if (head==null||head.next==null){
            return head;
        }
        Stack<ListNode> stack=new Stack<>();
        ListNode node=new ListNode(-1);
        ListNode node1=node;
        while (head!=null){
            stack.push(head);
            head=head.next;
            if (k==stack.size()){
                while (!stack.isEmpty()){
                    node1.next=stack.pop();
                    node1=node1.next;
                }
            }
        }
        if (!stack.isEmpty()){
            for (ListNode n1:stack) {
                node1.next=n1;
                node1=node1.next;
            }
        }
        node1.next=null;
        return node.next;
    }
    public int search(int[] nums, int target) {
        if (nums==null||nums.length==0){
            return 0;
        }
        int count=0;
        for (int num:nums) {
            if (num==target){
                count++;
            }
        }
        return count;
    }
}
